package com.maxima.maximaAttendance.daos;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import com.maxima.maximaAttendance.pojos.Project;
import com.maxima.maximaAttendance.utils.HibernateUtils;

public class ProjectDAO {
	
	Map<String, Object> map;
	
	public Project add(Project project) {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		session.save(project);
		session.getTransaction().commit();
		session.close();
		return project;
	}

	@SuppressWarnings("unchecked")
	public List<Project> getAll()
	{
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		List<Project> projects = null;
		try {
			projects = (List<Project>)session.createQuery("select p from Project p order by p.projectName").list();
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		session.close();
		return projects;
	}
	
	public Project get(Integer id)
	{
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		Project project = null;
		try {
			project = (Project)session.createQuery("select p from Project p where p.id="+ id).uniqueResult();
			
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		session.close();
		return project;
	}
	
	public void update(Integer id,Project newProject) {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		Project existingProject = null;
		try {
			existingProject = (Project)session.createQuery("select p from Project p where p.id="+ id).uniqueResult();
			existingProject.setProjectName(newProject.getProjectName());
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		session.close();
	}
	
}
